package cn.biplam.common.file.importfile.sheet;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import cn.biplam.common.client.Log;
import cn.biplam.common.client.Log.Log_Level;
import cn.biplam.common.file.ExcelFile;
import cn.biplam.common.file.importfile.ExcelFileSheet;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;

/**
 * sheet中的字段信息对象
 * @author WangSong 2009-7-28下午04:15:32
 * @version 1.0
 */
public class FieldSheet {
	
	/** 列信息<number,<列属性名,列属性值>>*/
	private KvMap<Integer,KvMap<String,String>> fields;
	/** 计数器*/
	private int count=-1;
	/** 行数*/
	private int rowSize;
	
	/**
	 * 构造函数
	 * @param sheet sheet对象
	 * @throws ErrorMessage
	 */
	public FieldSheet(HSSFSheet sheet,int rowSize) throws ErrorMessage{
		this.rowSize = rowSize;
		initUnionCell(sheet);
	}
	
	/**
	 * 不处理合并单元格
	 * @param sheet
	 */
	@SuppressWarnings("unused")
	private void init(HSSFSheet sheet) {
		/** 取字段信息*/
		if(fields==null){
			fields = new KvMap<Integer, KvMap<String,String>>();
			/**列名*/
			String columnName = null;
			/**列中文名*/
			String columnCName = null;
			/**列说明*/
			String columnComment = null;
			/**列名不为空的计数器*/
			int c = 0;
			KvMap<String,String> colInfo = null;
			for(int i=4;i<rowSize;i++){
				try {
					columnName = ExcelFile.getCellContent(sheet, i, 1);
					columnCName = ExcelFile.getCellContent(sheet, i, 2);
					columnComment = ExcelFile.getCellContent(sheet, i, 3);
					if(columnName!=null && !"".equals(columnName.trim())){
						colInfo = new KvMap<String, String>();
						colInfo.put(ExcelFileSheet.Name.ColumnName.toString(), columnName);
						colInfo.put(ExcelFileSheet.Name.ColumnCName.toString(), columnCName);
						colInfo.put(ExcelFileSheet.Name.ColumnComment.toString(), columnComment);
						fields.put(c, colInfo);
						c++;
					}
				} catch (ErrorMessage e) {
					Log.write(e,Log_Level.Error);
				}
			}
		}
	}
	
	/**
	 * 处理合并单元格(只处理行的单元格合并)
	 * @param sheet
	 */
	private void initUnionCell(HSSFSheet sheet) {
		/** 取字段信息*/
		if(fields==null){
			fields = new KvMap<Integer, KvMap<String,String>>();
			/**列名*/
			String columnName = null;
			/**列中文名*/
			String columnCName = null;
			/**列说明*/
			String columnComment = null;
			/**列名不为空的计数器*/
			int c = 0;
			KvMap<String,String> colInfo = null;
			KvMap<String, String> colInfoTemp = null;
			for(int i=4;i<rowSize;i++){
				try {
					columnName = ExcelFile.getCellContent(sheet, i, 1);
					columnCName = ExcelFile.getCellContent(sheet, i, 2);
					columnComment = ExcelFile.getCellContent(sheet, i, 3);
					if(columnName!=null && !"".equals(columnName.trim())){
						colInfo = new KvMap<String, String>();
						colInfoTemp = colInfo;
						colInfo.put(ExcelFileSheet.Name.ColumnName.toString(), columnName);
						colInfo.put(ExcelFileSheet.Name.ColumnCName.toString(), columnCName);
						colInfo.put(ExcelFileSheet.Name.ColumnComment.toString(), columnComment);
						fields.put(c, colInfo);
						c++;
					}else{
						if(columnName!=null){
							colInfoTemp.put(ExcelFileSheet.Name.ColumnName.toString(),
									colInfoTemp.getByKey(ExcelFileSheet.Name.ColumnName.toString())+columnName);
						}
						if(columnCName!=null){
							colInfoTemp.put(ExcelFileSheet.Name.ColumnCName.toString(),
									colInfoTemp.getByKey(ExcelFileSheet.Name.ColumnCName.toString())+columnCName);
						}
						if(columnComment!=null){
							colInfoTemp.put(ExcelFileSheet.Name.ColumnComment.toString(),
									colInfoTemp.getByKey(ExcelFileSheet.Name.ColumnComment.toString())+columnComment);
						}
					}
				} catch (ErrorMessage e) {
					Log.write(e,Log_Level.Error);
				}
			}
		}
	}
	
	/**
	 * 是否有sheet
	 * @return
	 */
	public boolean haveNext(){
		if(count<fields.size()-1){
			return true;
		}
		return false;
	}

	/**
	 * 指针下移
	 */
	public void next(){
		count++;
	}
	
	/**
	 * 取列名
	 * @return 列名
	 */
	public String getName(){
		return fields.getByKey(count).
		   getByKey(ExcelFileSheet.Name.ColumnName.toString());
	}
	
	/**
	 * 取列中文名
	 * @return 列中文名
	 */
	public String getCName(){
		return fields.getByKey(count).
		   getByKey(ExcelFileSheet.Name.ColumnCName.toString());
	}
	
	/**
	 * 取列说明
	 * @return 列说明
	 */
	public String getComment(){
		return fields.getByKey(count).
		   getByKey(ExcelFileSheet.Name.ColumnComment.toString());
	}
	
	/**
	 * 取列数据类型
	 * @return 数据类型
	 */
	public String getDataType(){
		return fields.getByKey(count).
		   getByKey(ExcelFileSheet.Name.ColumnDataType.toString());
	}
	
	/**
	 * 取是否允许为空
	 * @return 是否允许为空 NOT NULL/NULL
	 */
	public String getNullAble(){
		return fields.getByKey(count).
		   getByKey(ExcelFileSheet.Name.ColumnNullAble.toString());
	}
	
	/**
	 * 取是否是主键
	 * @return 是否是主键 Y/N
	 */
	public String getIsPk(){
		return fields.getByKey(count).
		   getByKey(ExcelFileSheet.Name.ColumnIsPk.toString());
	}
	
	/**
	 * 取是否是外键
	 * @return 是否是外键 Y/N
	 */
	public String getIsFk(){
		return fields.getByKey(count).
		   getByKey(ExcelFileSheet.Name.ColumnIsFk.toString());
	}	
}
